package com.yunxi.service.localService.impl.sto;

import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.dao.sto.StoCarriageRouteMapper;
import com.yunxi.model.sto.StoCarriageRoute;
import com.yunxi.service.localService.sto.StoCarriageRouteService;
import com.yunxi.sto.pojo.StoCarriageRouteDetailVO;
import com.yunxi.sto.pojo.StoCarriageRouteVO;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运输路线 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-15
 */
@Service
public class StoCarriageRouteServiceImpl extends ServiceImplPlus<StoCarriageRouteMapper, StoCarriageRoute> implements StoCarriageRouteService {

    @Override
    public void delAndReset(List<String> list, Integer isDel,String userId) {
        for (String id : list){
            StoCarriageRoute stoCarriageRoute = baseMapper.selectById(id);
            stoCarriageRoute.setIsDel(isDel);
            stoCarriageRoute.setUpdateBy(userId);
            baseMapper.updateById(stoCarriageRoute);
        }
    }

    /**
     * 批量审批运输路线
     * @param list
     */
    @Override
    public void approve(List<StoCarriageRouteVO> list,String userId) throws Exception{
        if(list != null && list.size() > 0){
            for (StoCarriageRouteVO stoCarriageRouteVO : list){
                StoCarriageRoute stoCarriageRoute = baseMapper.selectById(stoCarriageRouteVO.getRouteId());
                if(stoCarriageRoute != null){
                    String approveState = stoCarriageRoute.getApproveState();
                    if(approveState != null && approveState.equals("2")){
                        throw new Exception("操作失败,已审核通过的单据不可以重复审核!");
                    }
                    stoCarriageRoute.setApproveState(stoCarriageRouteVO.getApproveState());
                    stoCarriageRoute.setUpdateBy(userId);
                    baseMapper.updateById(stoCarriageRoute);
                }
                else {
                    throw new Exception("操作失败,无法根据主键id:"+stoCarriageRouteVO.getRouteId()+"找到对应路线!");
                }
            }
        }
    }

    /**
     * 获取路线列表
     * @return
     */
    @Override
    public List getList() {
        return baseMapper.getList();
    }

    @Override
    public List carrierList(String routeId,String carriageType) {
        Map<String, String> map = new HashMap<>();
        map.put("ROUTE_ID",routeId);
        map.put("CARRIAGE_TYPE",carriageType);
        return baseMapper.carrierList(map);
    }

    @Override
    public List<StoCarriageRouteDetailVO> getCarrierById(String routeId) {
        List<StoCarriageRouteDetailVO> list = baseMapper.getCarrierById(routeId);
        return list;
    }

    /**
     * 清空路线详情
     * @param routeId
     */
    @Override
    public void removeRouteDetail(String routeId) {
        baseMapper.removeRouteDetail(routeId);
    }
}
